package com.mukefile.action;

import com.alibaba.fastjson.JSON;
import com.mukefile.data.bo.FileBo;
import com.mukefile.dto.KnsFile;
import com.mukefile.pojo.ResponseData;
import com.mukefile.pojo.User;
import com.mukefile.service.SysProfileService;
import com.mukefile.service.UploadService;
import com.mukefile.utils.CommonUtil;
import com.mukefile.utils.constant.Constant;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xinke.huang@hand-china.com
 * @version 1.0
 * @date 2021/2/9
 */
@Controller
@Scope("prototype")
public class UploadAction extends ActionSupport implements ModelDriven<KnsFile>{
    private final KnsFile knsFile = new KnsFile();
    private Long folderId;

    public Long getFolderId() {
        return folderId;
    }

    public void setFolderId(Long folderId) {
        this.folderId = folderId;
    }

    @Resource
    UploadService uploadService;
    /**
     * 获取Session对象
     */
    private final Map<String,Object> session = ActionContext.getContext().getSession();

    @Override
    public KnsFile getModel() {
        return this.knsFile;
    }
    public void uploadFile(){

        User user = (User) session.get(Constant.SESSION_USER);
        FileBo fileBo = new FileBo();
        fileBo.setKnsFile(this.knsFile);
        fileBo.setUserId(user.getId());
        fileBo.setFolderId(folderId);
        try {
            this.uploadService.uploadFile(fileBo);
        } catch (Exception e) {
            try {
                CommonUtil.responseJSON("error");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            e.printStackTrace();
        }
    }



}
